﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;
using HYCreateDuct.FormDuct;
using HYCreateDuct.MethodCreateDuct;
using HYView;

namespace HYCreateDuct
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class cmdCreateVerticalDuct : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			if (SwitchView.SetUsableView(CmdViewType.FloorPlan, commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			if (cmdCreateVerticalDuct.hWndRevitS == null)
			{
				cmdCreateVerticalDuct.hWndRevitS = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			List<ViewType> viewTypes = YJKRevitTools.ListViewNeed(1);
			if (!YJKRevitTools.ViewCheck(activeUIDocument, viewTypes))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			FormCreateDuct formCreateDuct = new FormCreateDuct(application, "绘制竖风管");
			formCreateDuct.Show(cmdCreateVerticalDuct.hWndRevitS);
			int wMsg = 7;
			cmdCreateVerticalDuct.SendMessage(cmdCreateVerticalDuct.hWndRevitS.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			try
			{
				new CreateJigLine().CreateDuctLineV(application, formCreateDuct);
			}
			catch
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static YJKWindowHandle hWndRevitS;
	}
}
